//
//  HWoftenwanView.m
//  HWanGame
//
//  Created by 陈道斌 on 2020/9/22.
//  Copyright © 2020 Chendaobin. All rights reserved.
//

#import "HWoftenwanView.h"
#import "HWjingchangGameTableViewCell.h"




@interface HWoftenwanView()<UIScrollViewDelegate,UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, weak) UIScrollView *contentScrollView;
@property (strong,nonatomic) UITableView *oneTabview;
@property (strong,nonatomic) UITableView *twotableView;
@property (strong,nonatomic) UITableView *threadetablview;

@end



@implementation HWoftenwanView



- (void)QC_refreshData {
    
    
  //  self.oneTabview.frame = CGRectMake(0, 0, self.frame.size.width, self.frame.size.)
    
    
    CGFloat x = self.xuanzhongTag * ([UIScreen mainScreen].bounds.size.width-40);
    self.contentScrollView.contentOffset = CGPointMake(x, 0);
    
    [self.oneTabview reloadData];
    [self.twotableView reloadData];
    [self.threadetablview reloadData];
    
}



- (instancetype)initWithFrame:(CGRect)frame {
    
    if (self =[super initWithFrame:frame]) {
           
           [self setUpView];
        
        
           
       }
       
       return  self;
    
    
    
}

- (void)layoutSubviews {
    
    [super layoutSubviews];
    
    self.contentScrollView.frame =  CGRectMake(0, 0, self.frame.size.width, self.frame.size.height);
    self.contentScrollView.tag = 100;
    
    self.oneTabview.frame =  CGRectMake(0, 0,self.frame.size.width, self.frame.size.height);
    self.twotableView.frame =  CGRectMake(self.frame.size.width, 0, self.frame.size.width, self.frame.size.height);
    self.threadetablview.frame = CGRectMake(self.frame.size.width*2, 0, self.frame.size.width, self.frame.size.height);

    NSLog(@"里面的高度 %lf",self.frame.size.height);
    
    
}




- (void)setUpView {
    
    [self setupContentScrollView];
    
    //添加三个tabview
    [self addTheradtabview];
    

    
}


#pragma mark - 添加内容滚动视图
- (void)setupContentScrollView
{
    // 创建contentScrollView
    UIScrollView *contentScrollView = [[UIScrollView alloc] init];
    CGFloat y = 0;
    contentScrollView.frame = CGRectMake(0, y, self.bounds.size.width, self.frame.size.height);
    [self addSubview:contentScrollView];
    
    _contentScrollView = contentScrollView;
    
    // 设置contentScrollView的属性
    // 分页
    self.contentScrollView.pagingEnabled = YES;
    // 弹簧
    self.contentScrollView.bounces = NO;
    // 指示器
    self.contentScrollView.showsHorizontalScrollIndicator = NO;
    
    contentScrollView.backgroundColor = [UIColor redColor];
    
    // 设置代理.目的:监听内容滚动视图 什么时候滚动完成
    self.contentScrollView.delegate = self;
    
    // 设置内容的滚动范围
    self.contentScrollView.contentSize = CGSizeMake(3 * kScreenW, 0);
    
    
    
}


-(void)addTheradtabview {
    
    
    self.oneTabview = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, kScreenW-40,  self.frame.size.height) style:UITableViewStylePlain];
    self.oneTabview.delegate = self;
    self.oneTabview.dataSource = self;
    self.oneTabview.tag = 10;
    self.oneTabview.scrollEnabled = NO;
    self.oneTabview.tableFooterView = [[UIView alloc]init];
    self.oneTabview.backgroundColor = [UIColor whiteColor];
    
     self.oneTabview.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.contentScrollView addSubview:self.oneTabview];
    
    
    
    
     self.twotableView = [[UITableView alloc]initWithFrame:CGRectMake(kScreenW-40, 0, kScreenW-40,   self.frame.size.height) style:UITableViewStylePlain];
     self.twotableView.delegate = self;
     self.twotableView.dataSource = self;
     self.twotableView.tag = 20;
     self.twotableView.scrollEnabled = NO;
     self.twotableView.backgroundColor = [UIColor whiteColor];
       self.twotableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
     self.twotableView.tableFooterView = [[UIView alloc]init];
     [self.contentScrollView addSubview: self.twotableView];
    
    
    
    self.threadetablview = [[UITableView alloc]initWithFrame:CGRectMake((kScreenW-40)*2, 0, kScreenW-40,   self.frame.size.height) style:UITableViewStylePlain];
    self.threadetablview.delegate = self;
    self.threadetablview.dataSource = self;
    self.threadetablview.scrollEnabled = NO;
    self.threadetablview.tag = 30;
    self.threadetablview.tableFooterView = [[UIView alloc]init];
    self.threadetablview.backgroundColor = [UIColor whiteColor];
    self.threadetablview.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.contentScrollView addSubview:self.threadetablview];
    
    
    
    
    
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    
    if (tableView.tag == 10) {
         return  self.data1Arr.count;
        
    } else if ( tableView.tag == 20) {
        return  self.data2Arr.count;
        
    } else {
        
         return  self.data3Arr.count;
    }
    
    return  20;
    
    
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    
    
    
    
   //   static NSString *cellID = @"cellID";
    
   //   HWGameListModel *model  = nil;
    if (tableView.tag == 10) {
        
           static NSString *cellID = @"cellID";
        
        NSDictionary *dic = self.data1Arr[indexPath.row];
       
        HWjingchangGameTableViewCell *cell =  [tableView dequeueReusableCellWithIdentifier:cellID];
        
        if (!cell) {
            cell =  [[NSBundle mainBundle]loadNibNamed:@"HWjingchangGameTableViewCell" owner:nil options:nil].firstObject;
            
            
        }
        
        cell.mingchengLabel.text = [NSString stringWithFormat:@"%@",dic[@"name"]];
        cell.pingfenlabel.text = [NSString stringWithFormat:@"%@",dic[@"point"]];
        cell.leixingLabel.text = [NSString stringWithFormat:@"%@",dic[@"type"]];
        [cell.icoiamgeView sd_setImageWithURL:[NSURL URLWithString:dic[@"icon"]] placeholderImage:nil];
        
        NSArray *arr = (NSArray * )dic[@"biaoqian"];
        
        cell.oneLabel.hidden = YES;
        cell.twoLabel.hidden = YES;
        cell.threadLabel.hidden = YES;
    
        
        for (int i = 0; i <  arr.count; i++ ) {
            if (i == 0 ) {
                
                 cell.oneLabel.text = [NSString stringWithFormat:@"%@",arr[i]];
                 cell.oneLabel.hidden = NO;
                
            } else if (i == 1) {
                
                   cell.twoLabel.text = [NSString stringWithFormat:@"%@",arr[i]];
                cell.twoLabel.hidden = NO;
                
                
            } else {
                
                   cell.threadLabel.text = [NSString stringWithFormat:@"%@",arr[i]];
                 cell.threadLabel.hidden = NO;
                
            }
            
            
             
        }
          cell.selectionStyle =  UITableViewCellSelectionStyleNone;
         
        return  cell ;
        
        
        
    } else if ( tableView.tag == 20) {
        
        NSDictionary *dic = self.data2Arr[indexPath.row];
        
         static NSString *cellID1 = @"cellID1";
         
        HWjingchangGameTableViewCell *cell =  [tableView dequeueReusableCellWithIdentifier:cellID1];
             
            if (!cell) {
                 cell =  [[NSBundle mainBundle]loadNibNamed:@"HWjingchangGameTableViewCell" owner:nil options:nil].firstObject;
                 
                 
             }
        
        
        
        cell.mingchengLabel.text = [NSString stringWithFormat:@"%@",dic[@"name"]];
              cell.pingfenlabel.text = [NSString stringWithFormat:@"%@",dic[@"point"]];
              cell.leixingLabel.text = [NSString stringWithFormat:@"%@",dic[@"type"]];
              [cell.icoiamgeView sd_setImageWithURL:[NSURL URLWithString:dic[@"icon"]] placeholderImage:nil];
              
              NSArray *arr = (NSArray * )dic[@"biaoqian"];
              
              cell.oneLabel.hidden = YES;
              cell.twoLabel.hidden = YES;
              cell.threadLabel.hidden = YES;
          
              
              for (int i = 0; i <  arr.count; i++ ) {
                  if (i == 0 ) {
                      
                       cell.oneLabel.text = [NSString stringWithFormat:@"%@",arr[i]];
                       cell.oneLabel.hidden = NO;
                      
                  } else if (i == 1) {
                      
                         cell.twoLabel.text = [NSString stringWithFormat:@"%@",arr[i]];
                      cell.twoLabel.hidden = NO;
                      
                      
                  } else {
                      
                         cell.threadLabel.text = [NSString stringWithFormat:@"%@",arr[i]];
                       cell.threadLabel.hidden = NO;
                      
                  }
                  
                  
                   
              }
        
        
        
        cell.selectionStyle =  UITableViewCellSelectionStyleNone;
        
        
        
        
        
           
          return  cell ;
          
         
        
    } else {
        
        
        
        NSDictionary *dic = self.data3Arr[indexPath.row];
            static NSString *cellID2 = @"cellID2";
          HWjingchangGameTableViewCell *cell =  [tableView dequeueReusableCellWithIdentifier:cellID2];
              
         if (!cell) {
              cell =  [[NSBundle mainBundle]loadNibNamed:@"HWjingchangGameTableViewCell" owner:nil options:nil].firstObject;
                  
                  
         }
        
          cell.selectionStyle =  UITableViewCellSelectionStyleNone;
        
        cell.mingchengLabel.text = [NSString stringWithFormat:@"%@",dic[@"name"]];
              cell.pingfenlabel.text = [NSString stringWithFormat:@"%@",dic[@"point"]];
              cell.leixingLabel.text = [NSString stringWithFormat:@"%@",dic[@"type"]];
              [cell.icoiamgeView sd_setImageWithURL:[NSURL URLWithString:dic[@"icon"]] placeholderImage:nil];
              
              NSArray *arr = (NSArray * )dic[@"biaoqian"];
              
              cell.oneLabel.hidden = YES;
              cell.twoLabel.hidden = YES;
              cell.threadLabel.hidden = YES;
          
              
              for (int i = 0; i <  arr.count; i++ ) {
                  if (i == 0 ) {
                      
                       cell.oneLabel.text = [NSString stringWithFormat:@"%@",arr[i]];
                       cell.oneLabel.hidden = NO;
                      
                  } else if (i == 1) {
                      
                         cell.twoLabel.text = [NSString stringWithFormat:@"%@",arr[i]];
                      cell.twoLabel.hidden = NO;
                      
                      
                  } else {
                      
                         cell.threadLabel.text = [NSString stringWithFormat:@"%@",arr[i]];
                       cell.threadLabel.hidden = NO;
                      
                  }
                  
                  
                   
              }
        
        
          cell.selectionStyle =  UITableViewCellSelectionStyleNone;
        
        
        
          return  cell ;
          
        
       
    }
    
    

        

    
    
    
 
    
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    
    
    return 62;
    
    
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    
    
    //  NSDictionary *dic = self.data3Arr[indexPath.row];
     if (tableView.tag == 10) {
         
           NSDictionary *dic = self.data1Arr[indexPath.row];
         
            HWGameDetailController  *xiangqing = [[HWGameDetailController alloc]init];
                     xiangqing.gameId =  dic[@"gid"];
               UINavigationController *na =  [self currentNC];

               [na pushViewController:xiangqing animated:YES];
         
         
         
         
     } else if (tableView.tag == 20 ) {
         
          NSDictionary *dic = self.data2Arr[indexPath.row];
         
         HWGameDetailController  *xiangqing = [[HWGameDetailController alloc]init];
                         xiangqing.gameId =  dic[@"gid"];
                   UINavigationController *na =  [self currentNC];

                   [na pushViewController:xiangqing animated:YES];
         
         
         
     } else {
         
         
         
          NSDictionary *dic = self.data3Arr[indexPath.row];
         
         HWGameDetailController  *xiangqing = [[HWGameDetailController alloc]init];
                         xiangqing.gameId =  dic[@"gid"];
         UINavigationController *na =  [self currentNC];

         [na pushViewController:xiangqing animated:YES];
         
         
         
         
     }
    
    
    
    
 
}


- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    
    NSInteger leftI = scrollView.contentOffset.x / (kScreenW-40);
    NSLog(@"54滑动 %ld",leftI);
    
    if (scrollView.tag == 100) {
        
        NSString *rolling = [NSString stringWithFormat:@"%ld",leftI];
        
        NSDictionary *dict = @{@"rolling":rolling,};
        
        [[NSNotificationCenter defaultCenter] postNotificationName:@"cenderSentimentOnTheSlide" object:nil userInfo:dict];
        
    }
   
    
    
}


- (UINavigationController *)currentNC
{
    if (![[UIApplication sharedApplication].windows.lastObject isKindOfClass:[UIWindow class]]) {
        NSAssert(0, @"未获取到导航控制器");
        return nil;
    }
    UIViewController *rootViewController = [UIApplication sharedApplication].keyWindow.rootViewController;
    return [self getCurrentNCFrom:rootViewController];
}

//递归
- (UINavigationController *)getCurrentNCFrom:(UIViewController *)vc
{
    if ([vc isKindOfClass:[UITabBarController class]]) {
        UINavigationController *nc = ((UITabBarController *)vc).selectedViewController;
        return [self getCurrentNCFrom:nc];
    }
    else if ([vc isKindOfClass:[UINavigationController class]]) {
        if (((UINavigationController *)vc).presentedViewController) {
            return [self getCurrentNCFrom:((UINavigationController *)vc).presentedViewController];
        }
        return [self getCurrentNCFrom:((UINavigationController *)vc).topViewController];
    }
    else if ([vc isKindOfClass:[UIViewController class]]) {
        if (vc.presentedViewController) {
            return [self getCurrentNCFrom:vc.presentedViewController];
        }
        else {
            return vc.navigationController;
        }
    }
    else {
        NSAssert(0, @"未获取到导航控制器");
        return nil;
    }
}




@end
